import $ from 'jquery';
import e from "./view";
import t from "./rect";
import n from "./view_factory";

class ViewContainer extends e {
  // TODO: r.prototype = new e,
  // r.prototype.constructor = r
  constructor() {
    super();
  }
  className = "AXEViewContainer";
  clone() {
    let e = this,
      t = new ViewContainer();
    return e.copyProperties(t), t;
  }
  copyProperties(t) {
    let n = this;
    e.prototype.copyProperties.call(n, t),
      t.muted(n.muted()),
      n.subViews().length > 0 &&
        n.eachSubView(function (e, n) {
          t.addSubView(n.clone());
        });
  }
  dealloc() {
    let t = this,
      n = t.subViews();
    for (let r = 0; r < n.length; r++) {
      let i = n[r];
      i.dealloc();
    }
    e.prototype.dealloc.call(t);
  }
  updatePropsFromXML(t) {
    let r = this;
    e.prototype.updatePropsFromXML.call(r, t),
      t.is("[muted]") && r.muted(t.attr("muted") == "true");
    let i = t.find("> *");
    i.each(function (e, t) {
      let i = $(t),
        s = i[0].nodeName.toLowerCase(),
        o = n.createViewByNodeName(s);
      o != null && (o.updatePropsFromXML(i), r.addSubView(o));
    });
  }
  needToRender(t) {
    let n = this,
      r,
      i = n.subViews();
    if (arguments.length > 0) r = e.prototype.needToRender.call(n, t);
    else {
      r = e.prototype.needToRender.call(n);
      if (r === !1)
        for (let s = 0; s < i.length; s++) {
          let o = i[s];
          if (o.needToRender()) {
            r = !0;
            break;
          }
        }
    }
    return r;
  }
  _subViews = null;
  subViews() {
    let e = this;
    return e._subViews == null && (e._subViews = []), e._subViews;
  }
  addSubView(e) {
    let t = this;
    e.parent() != null && e.parent().moveSubView(e),
      t.subViews().push(e),
      t.afterSubViewAdded(e);
  }
  insertSubViewAt(e, t) {
    let n = this;
    e.parent() != null && e.parent().moveSubView(e);
    let r = n.subViews();
    (t = Math.max(0, Math.min(r.length - 1, t))),
      r.splice(t, 0, e),
      n.afterSubViewAdded(e);
  }
  afterSubViewAdded(e) {
    let t = this;
    e.parent(t),
      e.onAdd(t),
      t.onStage && e.onAddToStage(),
      e.active(t.active()),
      t.needToRender(!0);
  }
  onAddToStage() {
    let t = this;
    e.prototype.onAddToStage.call(t);
    for (let n = 0; n < t.subViews().length; n++) {
      let r = t.subViews()[n];
      r.onAddToStage();
    }
  }
  empty() {
    let e = this,
      t = e.subViews().slice(0);
    for (let n = 0; n < t.length; n++) e.removeSubView(t[n]);
  }
  moveSubView(e) {
    let t = this,
      n = !1;
    if (e.parent() == t)
      for (let r = 0; r < t.subViews().length; r++) {
        let i = t.subViews()[r];
        if (i === e) {
          t.subViews().splice(r, 1), i.parent(null), (n = !0);
          break;
        }
      }
    return n && t.needToRender(!0), n;
  }
  removeSubView(e) {
    let t = this,
      n = t.moveSubView(e);
    n && e.onRemoveFromSuperView(t);
  }
  removeFromStage() {
    let t = this;
    e.prototype.removeFromStage.call(t),
      t.eachSubView(function (e, t) {
        t.removeFromStage();
      });
  }
  indexOfSubView(e) {
    let t = this,
      n = -1;
    for (let r = 0; r < t.subViews().length; r++) {
      let i = t.subViews()[r];
      if (i === e) {
        n = r;
        break;
      }
    }
    return n;
  }
  subViewByIndex(e) {
    let t = this,
      n = null;
    return e >= 0 && e < t.subViews().length && (n = t.subViews()[e]), n;
  }
  subViewByName(e) {
    let t = this,
      n = null;
    for (let r = 0; r < t.subViews().length; r++) {
      let i = t.subViews()[r];
      if (i.name() == e) {
        n = i;
        break;
      }
    }
    return n;
  }
  containsSubView(e) {
    let t = this,
      n = !1;
    return (
      t.eachSubView(function (t, r) {
        return r == e ? ((n = !0), !1) : !0;
      }),
      n
    );
  }
  visibleSubViews() {
    let e = this,
      t = [],
      n = e.rect(e);
    for (let r = 0; r < e.subViews().length; r++) {
      let i = e.subViews()[r];
      if (i.visible()) {
        let s = i.rect(e);
        (e.clip() === !1 || n.intersect(s).isEmpty() === !1) && t.push(i);
      }
    }
    return t;
  }
  eachSubView(e) {
    let t = this,
      n = t.subViews().slice();
    for (let r = 0; r < n.length; r++) {
      let i = n[r],
        s = e.call(null, r, i);
      if (s === !1) break;
    }
  }
  _muted = !1;
  muted(e?) {
    let t = this;
    return typeof e == "boolean" && e != t._muted && (t._muted = e), t._muted;
  }
  triggerSubViewEvent(e) {
    let t = this,
      n = !1,
      r = t.subViews();
    for (let i = r.length - 1; i >= 0; i--) {
      let s = r[i],
        o = s.triggerEvent(e);
      if (o) {
        n = o;
        break;
      }
    }
    return n;
  }
  triggerEvent(t) {
    let n = this,
      r = !1;
    return (
      n.muted()
        ? n.enabled() &&
          n.animating() === !1 &&
          n.visible() &&
          (r = n.triggerSubViewEvent(t))
        : ((r = e.prototype.triggerEvent.call(n, t)),
          r && n.triggerSubViewEvent(t)),
      r
    );
  }
  active(t?) {
    let n = this,
      r = e.prototype.active.call(n, t);
    return (
      n.eachSubView(function (e, t) {
        t.active(r);
      }),
      r
    );
  }
  onWindowOrientationChanged(t) {
    let n = this;
    e.prototype.onWindowOrientationChanged.call(n, t),
      n.eachSubView(function (e, n) {
        n.onWindowOrientationChanged(t);
      });
  }
  resize(t, n) {
    let r = this;
    e.prototype.resize.call(r, t, n),
      r.eachSubView(function (e, r) {
        r.resize(t, n);
      });
  }
  onAnimationStart() {
    let t = this;
    e.prototype.onAnimationStart.call(t);
    let n = t.subViews();
    for (let r = 0; r < n.length; r++) {
      let i = n[r];
      i.onAnimationStart();
    }
  }
  onAnimationStop() {
    let t = this;
    e.prototype.onAnimationStop.call(t);
    let n = t.subViews();
    for (let r = 0; r < n.length; r++) {
      let i = n[r];
      i.onAnimationStop();
    }
  }
  update(t, n) {
    let r = this;
    e.prototype.update.call(r, t, n);
    if (r.cachedImage == null) {
      let i = r.subViews();
      for (let s = 0; s < i.length; s++) {
        let o = i[s];
        o.visible() && o.update(t, n);
      }
    }
  }
}

export default ViewContainer;
